package com.qf.ssm01.servcie.impl;

import com.qf.ssm01.dao.UserDao;
import com.qf.ssm01.entity.User;
import com.qf.ssm01.servcie.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public User findUser(Integer id) {

        // service 层的业务逻辑
        if(id < 0){
            System.out.println("id 无效");
            User user = new User();
            user.setId(-1);
            user.setName("无效的用户id");
            return user;
        }
        User user = userDao.getUserById(id);


        return user;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean checkUser(User user) {

        String name = user.getName();
        String password = user.getPassword();
        // 根据user对象中的用户名，去数据库中查找有没有对应的用户
        // 如果没有查询到对于的用户，则返回false
        User dbUser = userDao.getUserByName(name);
        if (dbUser == null) {
            return false;
        }
        // 如果查询到了用户，则比较数据库中的用户的密码和当前user对象中的密码是否一致
        // 如果密码一致，则登录成功，否则登录失败
        String dbPassword = dbUser.getPassword();
        if(dbPassword.equals(password)){
            return true;
        }
        return false;
    }

}
